﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.orderData
{
   public  class materialListBLL
    {
        private materialListTableAdapter  _materialListAdapter = null;
        private materialListTableAdapter  materialListAdapter
        {
            get
            {
                if (_materialListAdapter == null) _materialListAdapter = new materialListTableAdapter();
                return _materialListAdapter;
            }
        }
        /// <summary>
        /// 查询原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.materialListDataTable getmaterialListAll()
        {
            try
            {
                return materialListAdapter.GetDatamaterialListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.materialListDataTable getmaterialListBymytypeAll()
        {
            try
            {
                return materialListAdapter.GetDatamaterialListBymytype ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.materialListDataTable getmaterialListBymtypeNoAll()
        {
            try
            {
                return materialListAdapter.GetDatamaterialListBymtypeNo ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据原材料编号查询原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.materialListDataTable getmaterialListBymatNamelikeAll(string matName)
        {
            try
            {
                return materialListAdapter.GetDatamaterialListBymatNamelike (matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据原材料编号查询原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.materialListDataTable getmaterialListBymatNamelikeNoAll(string matName)
        {
            try
            {
                return materialListAdapter.GetDatamaterialListBymatNamelikeNo (matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据原材料编号查询原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.materialListDataTable getmaterialListAllBymatNum(string matNum)
        {
            try
            {
                return materialListAdapter.GetDatamaterialListBymatNum (matNum);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 插入原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int materialListInsert(materialListModel row,string type)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                materialListDAL materialList = new materialListDAL(tn, conn);
                if (type == "五金")
                {
                    basListDAL basList = new basListDAL(tn, conn);
                    string orderSerial = basicSql.getOrderSerail("五金编号");
                    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "五金编号");
                }
                materialList.materialListInsert(row);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个原材料编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 导入原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int materialListimoportInsert(orderDataSet.materialListDataTable materialTable)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                materialListDAL materialList = new materialListDAL(tn, conn);
                materialList.materialListimportInsert (materialTable);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个原材料编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int materialListUpdate(materialListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                materialListDAL materialList = new materialListDAL(tn ,conn );
                materialList.materialListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行原材料产品表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int materialListDel(string matNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                materialListDAL materialList = new materialListDAL(tn, conn);
                materialList.materialListDel(matNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
